/* eslint-disable */
import vue from 'vue'
import Confirm from './Confirm.vue'

const ConfirmConstructor = vue.extend(Confirm)

const confirmDom = new ConfirmConstructor({
  el: document.createElement('div'),
  data () {
    return {
      isShow: false,
      tit: '',
      info: '',
      leftBtn: '是',
      rightBtn: '否',
      isLeftSelect: false,
      isRightSelect: false,
      isLeftClose: false,
      isRightClose: false,
    }
  },

  methods: {
    close () {
      this.isShow = false
    },

    leftCb (isClose) {
      if (confirmDom._leftCb) {
        this.isRightSelect = false;
        this.isLeftSelect = true;
        confirmDom._leftCb()
      }
      confirmDom.isLeftClose && this.close();
    },

    rightCb (isClose) {
      if (confirmDom._rightCb) {
        this.isLeftSelect = false;
        this.isRightSelect = true;
        confirmDom._rightCb()
      }
      confirmDom.isRightClose && this.close();
    }
  }
})

document.body.appendChild(confirmDom.$el)

const $confirm = {
  info (obj) {
    if (obj) {
      confirmDom.tit = obj.tit ? obj.tit : confirmDom.tit
      confirmDom.info = obj.info ? obj.info : confirmDom.info
      confirmDom.leftBtn = obj.leftBtn ? obj.leftBtn : confirmDom.leftBtn
      confirmDom.rightBtn = obj.rightBtn ? obj.rightBtn : confirmDom.rightBtn

      confirmDom._leftCb = obj.leftCb ? obj.leftCb : null
      confirmDom._rightCb = obj.rightCb ? obj.rightCb : null

      confirmDom.isLeftSelect = obj.isLeftSelect ? obj.isLeftSelect : false
      confirmDom.isRightSelect = obj.isRightSelect ? obj.isRightSelect : false

      confirmDom.isLeftClose = obj.isLeftClose ? obj.isLeftClose : false
      confirmDom.isRightClose = obj.isRightClose ? obj.isRightClose : false
    }

    confirmDom.isShow = true
  }
}

export default function install (Vue) {
  Vue.prototype.$confirm = $confirm
}
